package view;

import java.awt.Dimension;
import java.util.Date;

import javax.swing.JPanel;

import model.Auction;
import model.AuctionCalendar;
import model.NonProfit;
import model.TimeObject;
import model.WestPanelEnum;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;

import controller.ACSystem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.border.LineBorder;

import java.awt.Color;

import com.toedter.calendar.JDateChooser;

/**
 * The AuctionInputPane Pane 
 * @author Bob Landowski
 */
@SuppressWarnings("serial")
public class AuctionInputPane extends JPanel {

	
	/** The duration */
	private int durationIn;
	
	/** The number of items in */
	private int numberOfItemsIn;
	
	/** The organization */ 
	private String my_organization;
	
	/** The first name */
	private String my_first_name;
	
	/** The last name */
	private String my_last_name;
	
	/** The phone */
	private String my_phone;
	
	/** My auction time */
	private TimeObject my_auction_time;
	
	/** My date 1 */
	private Date my_date1;
	
	/** My star time in hours */
	private JTextField startTimeHr;
	
	/** My duration text field */
	private JTextField duration;
	
	/** The number of items text field */
	private JTextField numberOfItems;
	
	/** My start time in minutes */
	private JTextField startTimeMin;
	
	/** My nonProfit */
	private NonProfit my_nonprofit;

	/** My org name label */
	private JLabel my_organization_name;
	
	/** My contact name label */
	private JLabel my_contact_name;
	
	/** My phone label */
	private JLabel my_contact_phone;
	
	/** My date chooser */
	private JDateChooser dateChooser;
	
	/** My start time in hours */
	private String my_startTimeHr;
	
	/** My start time in minutes */
	private String my_startTimeMin;
	
	/** My auction */
	private Auction my_auction;

	/** My AC controller */
	private ACSystem my_ACcontroller;
	
	/**
	 * Create the panel.
	 */
	public AuctionInputPane(final ACSystem the_ACcontroller) {
		setBorder(new LineBorder(new Color(0, 0, 0), 5));
		my_ACcontroller = the_ACcontroller;
		setLayout(null);
		setPreferredSize(new Dimension(400, 450));

		JLabel auctionRequestLabel = new JLabel("Auction Request");
		auctionRequestLabel.setBounds(149, 16, 135, 20);
		add(auctionRequestLabel);

		JLabel organizationLabel = new JLabel("Organization");
		organizationLabel.setBounds(15, 64, 99, 20);
		add(organizationLabel);

		my_organization_name = new JLabel(my_organization);
		my_organization_name.setBounds(159, 64, 173, 20);
		my_organization_name.setText(my_organization);
		add(my_organization_name);

		JLabel contactPersonLabel = new JLabel("Contact person:");
		contactPersonLabel.setBounds(15, 94, 117, 20);
		add(contactPersonLabel);

		my_contact_name = new JLabel(my_first_name + " " + my_last_name);
		my_contact_name.setBounds(159, 94, 198, 20);
		add(my_contact_name);

		JLabel contactPhoneLabel = new JLabel("Contact Phone:");
		contactPhoneLabel.setBounds(15, 124, 117, 20);
		add(contactPhoneLabel);

		my_contact_phone = new JLabel(my_phone);
		my_contact_phone.setBounds(159, 124, 135, 20);
		add(my_contact_phone);

		JLabel lblDate = new JLabel("Auction Date:");
		lblDate.setBounds(15, 154, 99, 20);
		add(lblDate);

		JLabel auctionStartTimeLabel = new JLabel("Auction start time:");
		auctionStartTimeLabel.setBounds(15, 184, 146, 20);
		add(auctionStartTimeLabel);

		startTimeHr = new JTextField();
		startTimeHr.setBounds(159, 184, 50, 20);
		add(startTimeHr);
		startTimeHr.setColumns(10);
		
		JLabel label = new JLabel(":");
		label.setBounds(215, 184, 14, 20);
		add(label);
		
		startTimeMin = new JTextField();
		startTimeMin.setBounds(224, 184, 50, 20);
		add(startTimeMin);
		startTimeMin.setColumns(10);

		JLabel auctionDurationLabel = new JLabel("Auction duration (in hours):");
		auctionDurationLabel.setBounds(15, 214, 205, 20);
		add(auctionDurationLabel);

		duration = new JTextField();
		duration.setBounds(230, 214, 146, 20);
		add(duration);
		duration.setColumns(10);

		JLabel AnticipatedNumberLabel = new JLabel("Anticipated number of items:");
		AnticipatedNumberLabel.setBounds(15, 244, 213, 20);
		add(AnticipatedNumberLabel);

		numberOfItems = new JTextField();
		numberOfItems.setBounds(230, 244, 146, 20);
		add(numberOfItems);
		numberOfItems.setColumns(10);

		JLabel commentsLabel = new JLabel("Comments:");
		commentsLabel.setBounds(15, 274, 99, 20);
		add(commentsLabel);

		JTextArea commentsTxt = new JTextArea();
		commentsTxt.setBounds(50, 304, 282, 80);
		add(commentsTxt);

		JButton submitButton = new JButton("Submit");
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveData();
			}
		});
		submitButton.setBounds(46, 400, 115, 29);
		add(submitButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				my_ACcontroller.setWestPanel(WestPanelEnum.CALENDAR);
				my_ACcontroller.getMainFrame().westPanelState(new Object());
			}
		});
		cancelButton.setBounds(205, 400, 115, 29);
		add(cancelButton);
		
		dateChooser = new JDateChooser();
		dateChooser.setBounds(129, 154, 155, 20);
		add(dateChooser);
	}

	/**
	 * Save the data 
	 */
	public void saveData(){

		my_date1 = dateChooser.getDate(); 
		my_startTimeHr = startTimeHr.getText();
		my_startTimeMin = startTimeMin.getText();
		durationIn = Integer.parseInt(duration.getText());
		my_auction_time = new TimeObject(Integer.parseInt(my_startTimeHr),Integer.parseInt(my_startTimeMin), durationIn);
		numberOfItemsIn = Integer.parseInt(numberOfItems.getText());
		my_auction = new Auction(my_date1, my_auction_time, durationIn,numberOfItemsIn, my_nonprofit);
		
		
		if(!AuctionCalendar.isTimeValid(my_auction_time, my_date1)){
			JOptionPane.showMessageDialog(my_ACcontroller.getMainFrame(), "Can not start an Auction with in 2 hours of each other",
					"Time not valid.",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		
		if (my_ACcontroller.getAuctionCalendar().hasAuction(my_auction.getOwner())) {
			JOptionPane.showMessageDialog(my_ACcontroller.getMainFrame(), "Only one Auction Allowed Per Year",
					"Date not valid.",
					JOptionPane.ERROR_MESSAGE);
		} else if (my_ACcontroller.getAuctionCalendar().isValidDate(my_date1)){
			if (my_ACcontroller.getAuctionCalendar().isFull()) {
				JOptionPane.showMessageDialog(my_ACcontroller.getMainFrame(), 
						"Sorry, we are currently experiencing a high volume of " +
						"auctions and cannot accept new requests.",
						"We're booked up!",
					
					JOptionPane.ERROR_MESSAGE);
			} else {
				my_ACcontroller.setWestPanel(WestPanelEnum.AUCTIONVERIFICATION);
				my_ACcontroller.getMainFrame().westPanelState(my_auction);
			}
		} else {
			JOptionPane.showMessageDialog(my_ACcontroller.getMainFrame(), "Date not available, Please try again",
					"Date not valid.",
					JOptionPane.ERROR_MESSAGE);
		}
	}
	

	/**
	 * Get object data.
	 * 
	 * @param the_object Returns an object will all the data
	 */
	public void getObjectData(Object the_object){
		my_nonprofit = (NonProfit)the_object;
		my_organization = my_nonprofit.getMy_organization();
		my_organization_name.setText(my_organization);
		my_first_name = my_nonprofit.get_first_name();
		my_last_name = my_nonprofit.get_last_name();
		my_contact_name.setText(my_first_name + " " + my_last_name);
		my_phone = my_nonprofit.getMy_phone();
		my_contact_phone.setText(my_phone);
	}
}